package logic

import (
	"context"
	"fmt"
	"rent/models"

	"rent/rentrpc/internal/svc"
	"rent/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectDeleteLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectDeleteLogic {
	return &CollectDeleteLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectDeleteLogic) CollectDelete(in *rentrpc.CollectDeleteRequest) (*rentrpc.CollectDeleteResponse, error) {
	// todo: add your logic here and delete this line
	var collect models.Collect
	tx := models.DB.Begin()
	tx.Where("commodity_id=?", in.CommodityId).Find(&collect)
	if collect.ID == 0 {
		tx.Callback()
		return nil, fmt.Errorf("商品未收藏")
	}
	models.DB.Delete(&collect)
	tx.Commit()
	return &rentrpc.CollectDeleteResponse{
		CollectId: int64(collect.ID),
	}, nil
}
